﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.ComponentModel.DataAnnotations;

namespace AspAuthentication.Core
{
    /// <summary>
    /// Defines the class Profile
    /// </summary>
   public class Profile
    {
        /// <summary>
        /// Gets or sets the username.
        /// </summary>
        /// <value>The username.</value>
        [Key]
        public string Username { get; set; }
        /// <summary>
        /// Gets or sets the name of the application.
        /// </summary>
        /// <value>The name of the application.</value>
        [Required, StringLength(250, MinimumLength = 1)]
        public string ApplicationName { get; set; }
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Profile"/> is authenticated.
        /// </summary>
        /// <value><c>true</c> if authenticated; otherwise, <c>false</c>.</value>
        [Required]
        public bool Authenticated { get; set; }
        /// <summary>
        /// Gets or sets the last activity date.
        /// </summary>
        /// <value>The last activity date.</value>
        [Required]
        public DateTime LastActivityDate { get; set; }
        /// <summary>
        /// Gets or sets the last updated date.
        /// </summary>
        /// <value>The last updated date.</value>
        [Required]
        public DateTime LastUpdatedDate { get; set; }
        /// <summary>
        /// Gets or sets the profile data.
        /// </summary>
        /// <value>The profile data.</value>
        public IList<ProfileItem> ProfileData { get; set; }

        /// <summary>
        /// Gets the property values.
        /// </summary>
        /// <value>The property values.</value>
        public SettingsPropertyValueCollection PropertyValues
        {
            get
            {
                var settingsPropertyValues = new SettingsPropertyValueCollection();

                foreach (var profileItem in ProfileData)
                {
                    var property = new SettingsProperty(profileItem.Name)
                                                    {
                                                        DefaultValue = "",
                                                        PropertyType = profileItem.GetType()
                                                    };
                    var spv = new SettingsPropertyValue(property) { PropertyValue = profileItem.Value };

                    settingsPropertyValues.Add(spv);
                }

                return settingsPropertyValues;
            }
        }

        /// <summary>
        /// Gets the settings context.
        /// </summary>
        /// <value>The settings context.</value>
        public SettingsContext SettingsContext
        {
            get
            {
                var context = new SettingsContext
                              {
                                  {"UserName", Username},
                                  {"IsAuthenticated", Authenticated},
                                  {"LastActivityDate",LastActivityDate},
                                  {"LastUpdatedDate",LastUpdatedDate}
                              };

                return context;
            }
        }

        /// <summary>
        /// Populates the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public Profile Populate(SettingsContext context)
        {
            Username = context["UserName"].ToString();
            Authenticated = Convert.ToBoolean(context["IsAuthenticated"]);

            DateTime temp;
            if (DateTime.TryParse(context["LastActivityDate"].ToString(), out temp))
            {
                LastActivityDate = temp;
            }
            if (DateTime.TryParse(context["LastUpdatedDate"].ToString(), out temp))
            {
                LastUpdatedDate = temp;
            }
            return this;
        }

        /// <summary>
        /// Populates the property data.
        /// </summary>
        /// <param name="settingsPropertyValues">The settings property values.</param>
        /// <returns></returns>
        public Profile PopulatePropertyData(SettingsPropertyValueCollection settingsPropertyValues)
        {
            ProfileData = new List<ProfileItem>();
            foreach (SettingsPropertyValue settingsPropertyValue in settingsPropertyValues)
            {
                ProfileData.Add(new ProfileItem
                                    {
                                        Name = settingsPropertyValue.Name,
                                        Value = settingsPropertyValue.PropertyValue.ToString(),
                                        Profile = this,
                                        ProfileUsername = Username
                                    });
            }
            return this;
        }
    }
}